package com.hsy.linemoney.common;

import android.support.annotation.NonNull;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by MartinKent on 2016/9/28.
 */

public class DoubleKeyMap<K1, K2, V> {
    private Map<K1, HashMap<K2, V>> map;

    public DoubleKeyMap() {
        map = new HashMap<>();
    }

    public void put(K1 key1, K2 key2, V value) {
        HashMap<K2, V> m;
        if (map.containsKey(key1)) {
            m = map.get(key1);
        } else {
            m = new HashMap<>();
            map.put(key1, m);
        }
        m.put(key2, value);
    }

    public V get(K1 key1, K2 key2) {
        if (!map.containsKey(key1) || !map.get(key1).containsKey(key2)) {
            return null;
        }
        return map.get(key1).get(key2);
    }

    public boolean containsKey(K1 key1, K2 key2) {
        return map.containsKey(key1) && map.get(key1).containsKey(key2);
    }

    public boolean containsValue(V value) {
        Set<Map.Entry<K1, HashMap<K2, V>>> set = map.entrySet();
        Iterator<Map.Entry<K1, HashMap<K2, V>>> iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry<K1, HashMap<K2, V>> entry = iterator.next();
            if (entry.getValue().containsValue(value)) {
                return true;
            }
        }
        return false;
    }

    public void clear() {
        map.clear();
    }
}
